<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/css/index.css">
    <link rel="stylesheet" href="/static/css/common.css">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/static/css/spec.css">
</head>
<style>
    input[disabled] {
        background: #eee;
        cursor: not-allowed;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <fieldset class="layui-elem-field">
                <legend><a style="color: #337ab7 !important;cursor: pointer;" lay-href="{:url('Product/product')}">商品管理</a> - 发布商品</legend>
                <div class="layui-field-box">
                    <div class="layui-card">
                        <div class="layui-card-header">发布商品
                            <!--<button class="layui-btn layui-btn-sm" style="float: right;margin-top: 6px;" onclick="javascript:history.back(-1);">返回</button>-->
                        </div>
                        <div class="layui-card-body" pad15>
                            <div class="layui-form" wid100 lay-filter="">
                                <input type="hidden" name="id" value="{$find.id}">
                                <input type="hidden" name="genus" value="{$find.genus}">
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">商品基本信息</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">分类</label>
                                    <div class="layui-input-inline" style="width: 100px;">
                                        <select name="cid">
                                            {volist name='cate' id='vo'}
                                            <option value="{$vo.id}" {eq name='find.cid' value='$vo.id'}selected{/eq}>{$vo.name}</option>
                                            {volist name='vo.lower' id='voo'}
                                            <option value="{$voo.id}" {eq name='find.cid' value='$voo.id'}selected{/eq}>&nbsp;&nbsp;&nbsp;{$voo.name}</option>
                                            {/volist}
                                            {/volist}
                                        </select>
                                    </div>
                                    <div lay-href="{:url('Product/category')}" style="height: 38px;line-height: 38px;color: #337ab7;">分类管理</div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品名称</label>
                                    <div class="layui-input-inline" style="width: 700px;">
                                        <input type="text" name="name" autocomplete="off" lay-verify="required" placeholder="请填写商品名称" value="{$find.name}" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品视频</label>
                                    <div class="layui-input-inline" style="width: 300px;">
                                        <input type="text" name="video" value="{$find.video}" placeholder="请上传视频" autocomplete="off" class="layui-input">
                                    </div>
                                    <button type="button" class="layui-btn" id="test5"><i class="layui-icon"></i>上传视频</button>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">是否推荐</label>
                                        <div class="layui-input-block">
                                            <input type="checkbox" name="is_tui" value="1" lay-text="推荐|默认" lay-skin="switch" {eq name="find.is_tui" value="1"}checked{/eq}>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">是否精品</label>
                                        <div class="layui-input-block">
                                            <input type="checkbox" name="is_jing" value="1" lay-text="精品|默认" lay-skin="switch" {eq name="find.is_jing" value="1"}checked{/eq}>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">是否新品</label>
                                        <div class="layui-input-block">
                                            <input type="checkbox" name="is_new" value="1" lay-text="新品|默认" lay-skin="switch" {eq name="find.is_new" value="1"}checked{/eq}>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品图片</label>
                                    <div class="layui-input-block">
                                        <p class="up-p" style="line-height: 38px;"><span class="up-span">最多可以上传5张图片</span></p>
                                        <div class="z_photo upimg-div clear">
                                            {volist name='img' id='vo'}
                                            <section class="up-section fl">
                                                <span class="up-span"></span>
                                                <img class="close-upimg" src="/static/img/a7.png">
                                                <img class="up-img" src="/uploads/{$vo.img}">
                                                <p class="img-name-p">{$vo.img}</p>
                                                <input id="taglocation" name="taglocation" value="" type="hidden">
                                                <input id="tags" name="tags" value="" type="hidden">
                                                <input type="text" style="display:none;" name="img" value="{$vo.img}">
                                            </section>
                                            {/volist}
                                            {if condition="count($img) > 4"}
                                            <section class="z_file fl" style="display: none;">
                                                <img src="/static/img/a11.png" class="add-img">
                                                <input style="cursor: pointer;" type="file" name="file" lay-verify="img" id="file" class="file" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" multiple />
                                            </section>
                                            {else /}
                                            <section class="z_file fl">
                                                <img src="/static/img/a11.png" class="add-img">
                                                <input style="cursor: pointer;" type="file" name="file" lay-verify="img" id="file" class="file" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" multiple />
                                            </section>
                                            {/if}
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品详情</label>
                                    <div class="layui-input-block">
                                        <script id="details" type="text/plain">
                                            {$find.details}
                                        </script>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">属性</label>
                                    <div class="layui-input-block">
                                        <input type="radio" name="type" value="0" title="线上" {if condition="$find eq ''"}checked{/if}{eq name='find.type' value='0'}checked{/eq}>
                                        <input type="radio" name="type" value="1" title="线下" {eq name='find.type' value='1'}checked{/eq}>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">所属商家</label>
                                    <div class="layui-input-inline" style="width: 400px;">
                                        <input type="text" class="layui-input" value="{$find.genus_name}" ts-selected="{$find.genus}" id="demo" lay-verify="required" placeholder="选择商家" autocomplete="off">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品服务</label>
                                    <div class="layui-input-inline" style="width: 400px;">
                                        <input type="text" name="service" autocomplete="off" placeholder="商品服务" value="{$find.service}" class="layui-input">
                                    </div>
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">规格/库存与价格</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">商品规格</label>
                                    <div class="layui-input-block" style="width: 700px;">
                                        <blockquote class="layui-elem-quote" style="background-color: #ececec;border: none;">
                                            {volist name='can' id='vo'}
                                            <div class="gui-content">
                                                <div class="layui-input-inline gui-inline">
                                                    <div class="gui-flex">
                                                        <div class="gui-selects">
                                                            <select name="spec" lay-filter="ziding">
                                                                <option value="颜色" {eq name="vo.spec" value="颜色"}selected{/eq}>颜色</option>
                                                                <option value="尺寸" {eq name="vo.spec" value="尺寸"}selected{/eq}>尺寸</option>
                                                                <option value="尺码" {eq name="vo.spec" value="尺码"}selected{/eq}>尺码</option>
                                                                <option value="规格" {eq name="vo.spec" value="规格"}selected{/eq}>规格</option>
                                                                <option value="容量" {eq name="vo.spec" value="容量"}selected{/eq}>容量</option>
                                                                <option value="含量" {eq name="vo.spec" value="含量"}selected{/eq}>含量</option>
                                                                <option value="款式" {eq name="vo.spec" value="款式"}selected{/eq}>款式</option>
                                                                <option value="种类" {eq name="vo.spec" value="种类"}selected{/eq}>种类</option>
                                                                <option value="型号" {eq name="vo.spec" value="型号"}selected{/eq}>型号</option>
                                                                <option value="品牌" {eq name="vo.spec" value="品牌"}selected{/eq}>品牌</option>
                                                                <option value="包装" {eq name="vo.spec" value="包装"}selected{/eq}>包装</option>
                                                                <option value="重量" {eq name="vo.spec" value="重量"}selected{/eq}>重量</option>
                                                                <option value="口味" {eq name="vo.spec" value="口味"}selected{/eq}>口味</option>
                                                                <option value="材质" {eq name="vo.spec" value="材质"}selected{/eq}>材质</option>
                                                                <option value="自定义" {eq name="vo.spec" value="自定义"}selected{/eq}>自定义</option>
                                                            </select>
                                                        </div>
                                                        {if condition="$vo.spec eq '自定义'"}
                                                        <div class="zidingyi">
                                                            <input class="layui-input" name="zidingyi" value="{$vo.bei}" lay-verify="required" style="border-color: rgb(201, 201, 201);">
                                                        </div>
                                                        {/if}
                                                        <div class="gui-picture">
                                                            <input type="checkbox" class="gupi" {if condition="$vo.value.img neq ''"}checked{/if} lay-filter="gupi" lay-skin="primary" title="添加规格图片">
                                                        </div>
                                                        <div class="guanbi" style="display: none;">X</div>
                                                    </div>
                                                </div>
                                                <div class="gui-flex">
                                                    {volist name='vo.value.name' id='voo'}
                                                    <div class="spec-relative">
                                                        <div class="zilei-close">X</div>
                                                        <div class="zilei">{$voo}</div>
                                                        {if condition="$vo.value.img neq ''"}
                                                        {if condition="$vo.value.img[$i-1]"}
                                                        <div class="add-picture">
                                                            <img src="/Uploads/{$vo.value.img[$i-1]}" style="width: 100%;height: 100%;">
                                                            <input type="hidden" name="upload" value="{$vo.value.img[$i-1]}">
                                                        </div>
                                                        {else/}
                                                        <div class="add-picture">+</div>
                                                        {/if}
                                                        {/if}
                                                    </div>
                                                    {/volist}
                                                    <div class="tianjia">+添加</div>
                                                </div>
                                            </div>
                                            {/volist}
                                            <button class="layui-btn" id="spec">添加规格</button>
                                            <button id="upload" style="display: none;"></button>
                                        </blockquote>
                                    </div>
                                </div>
                                <div class="layui-form-item kucun" style="display: none;">
                                    <label class="layui-form-label">商品库存</label>
                                    <div class="layui-input-block" style="width: 700px;">
                                        <blockquote class="layui-elem-quote" id="kucun" style="background-color: #ffffff;border: 1px solid #e5e5e5;">
                                            <table class="layui-table" id="tables">
                                                <thead>
                                                {if condition="$can neq ''"}
                                                <tr>
                                                    {volist name='can' id='vo'}
                                                    {if condition="$vo.spec eq '自定义'"}
                                                    <th>{$vo.bei}</th>
                                                    {else/}
                                                    <th>{$vo.spec}</th>
                                                    {/if}
                                                    {/volist}
                                                    <th>价格</th>
                                                    <th>库存</th>
                                                    <th>成本价</th>
                                                    <th>重量(kg)</th>
                                                </tr>
                                                {/if}
                                                </thead>
                                                <tbody>
                                                {volist name='zhi' id='vo'}
                                                <tr data-group="{$vo.group}">
                                                    {volist name='vo.name' id='voo'}
                                                    <td>{$voo}</td>
                                                    {/volist}
                                                    <td><input type="number" name="price" value="{$vo.price}" class="layui-input table-input" lay-verify="required|number"></td>
                                                    <td><input type="number" name="stock" value="{$vo.stock}" class="layui-input table-input" lay-verify="required|number"></td>
                                                    <td><input type="number" name="cost" value="{$vo.cost}" class="layui-input table-input" lay-verify="required|number"></td>
                                                    <td><input type="number" name="weight" value="{$vo.weight}" class="layui-input table-input" lay-verify="required|number"></td>
                                                </tr>
                                                {/volist}
                                                <tr class="shezhi">
                                                    <td colspan="{if condition='$colspan'}{$colspan}{else/}4{/if}">
                                                        批量设置：
                                                        <div class="repertory_tableoption">
                                                            <a href="javascript:;" data-input="sku_price">价格</a>
                                                            <a href="javascript:;" data-input="sku_stock">库存</a>
                                                            <a href="javascript:;" data-input="sku_cost">成本价</a>
                                                            <a href="javascript:;" data-input="sku_weight">重量</a>
                                                        </div>
                                                        <div class="repertory_tableform" style="display: none;">
                                                            <div style="float:left;">
                                                                <input type="text" class="layui-input jujiao" style="width: 120px;height: 30px;">
                                                            </div>
                                                            <div style="float:left;margin-left:8px;">
                                                                <a class="setok" href="javascript:;">保存</a>
                                                                <a class="setcancel" href="javascript:;">取消</a>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                        </blockquote>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">总库存</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" id="stock" name="stock" {if condition="$can neq ''"}disabled{/if} autocomplete="off" lay-verify="required|number" value="{$find.stock}" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">商品销量</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" name="volume" autocomplete="off" lay-verify="required|number" value="{$find.volume}" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">商品重量(kg)</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" id="weight" name="weight" {if condition="$can neq ''"}disabled{/if} autocomplete="off" lay-verify="required|number" value="{$find.weight}" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">商品价格(元)</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" id="price" name="price" {if condition="$can neq ''"}disabled{/if} autocomplete="off" lay-verify="required|number" value="{$find.price}" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">批发价格</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" name="p_price" autocomplete="off" value="{$find.p_price}" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label">起批量</label>
                                        <div class="layui-input-inline" style="width: 100px;">
                                            <input type="number" name="liang" autocomplete="off" value="{$find.liang}" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">运费与物流</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">运费设置</label>
                                    <div class="layui-input-inline" style="width: 200px;">
                                        <select name="ftype" lay-filter="yunfei">
                                            <option value="0" {eq name='find.ftype' value='0'}selected{/eq}>卖家包邮</option>
                                            <option value="1" {eq name='find.ftype' value='1'}selected{/eq}>统一运费</option>
                                        </select>
                                    </div>
                                    {if condition="$find.ftype eq '1'"}
                                    <div class="layui-input-inline" style="width: 100px;">
                                        <input type="number" name="freight" autocomplete="off" lay-verify="required|number" value="{$find.freight}" class="layui-input" style="padding-right:20px;">
                                        <span style="position:absolute;right:5px;top:8px;color:#b9b9b9;">元</span>
                                    </div>
                                    {/if}
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">卡密设置</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">卡密开关</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" lay-filter="is_open" name="is_open" value="1" lay-text="开启|关闭" lay-skin="switch" {eq name="find.is_open" value="1"}checked{/eq}>
                                    </div>
                                </div>
                                <div class="layui-form-item kalman layui-hide">
                                    <label class="layui-form-label">卡密管理</label>
                                    <div class="layui-input-block" style="">
                                        <textarea class="layui-textarea" name="kalman" style="width: 300px;display: inline-block;" lay-verify="che0">{$find.kalman}</textarea>
                                        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" style="vertical-align: top;" id="kalman">生成卡密</button>
                                    </div>
                                </div>
                                <div class="layui-form-item kalman layui-hide">
                                    <label class="layui-form-label">卡密价值</label>
                                    <div class="layui-input-block">
                                        <input type="radio" value="0" name="offset" title="商品价格" {if condition="!$find"}checked{/if}{eq name="find.offset" value="0"}checked{/eq}>
                                        <input type="radio" value="1" name="offset" title="自定义 <input type='number' name='ka' lay-verify='che1' class='layui-input' style='display: inline-block;width: 80px;' {gt name='find.offset' value='0'}value='{$find.offset}'{/gt}>" {gt name="find.offset" value="0"}checked{/gt}>
                                    </div>
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">分销管理</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">分销开关</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" lay-filter="share" name="share" value="1" lay-text="开启|关闭" lay-skin="switch" {eq name="find.share" value="1"}checked{/eq}>
                                    </div>
                                </div>
                                <div id="share" style="display: none;">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">分销图片</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="poster" value="{$find.poster}" placeholder="请上传图片" autocomplete="off" class="layui-input">
                                        </div>
                                        <button style="float: left;" type="button" class="layui-btn" id="demo3">上传图片</button>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">分销佣金</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="gold" value="{$find.gold}" placeholder="请输入分享奖励佣金" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">分销昵称颜色</label>
                                        <div class="layui-input-inline" style="width: 120px;">
                                            <input type="text" name="color" value="{$find.color}" placeholder="请选择颜色" class="layui-input" id="test-form-input">
                                        </div>
                                        <div class="layui-inline" style="left: -11px;">
                                            <div id="test-form"></div>
                                        </div>
                                    </div>
                                </div>
                                <fieldset class="layui-elem-field layui-field-title">
                                    <legend style="margin-left: 47px;">替换收费(邮费)名称</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">替换开关</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" lay-filter="replace_open" name="replace_open" value="1" lay-text="开启|关闭" lay-skin="switch" {eq name="find.replace_open" value="1"}checked{/eq}>
                                    </div>
                                </div>
                                <div id="replace" style="display: none;">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">替换名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="replace_name" value="{$find.replace_name}" placeholder="替换名称" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <!--<div class="layui-form-item">-->
                                        <!--<label class="layui-form-label">替换金额</label>-->
                                        <!--<div class="layui-input-inline">-->
                                            <!--<input type="text" name="replace_price" value="{$find.replace_price}" placeholder="替换金额" autocomplete="off" class="layui-input">-->
                                        <!--</div>-->
                                    <!--</div>-->
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        {if condition="$find eq ''"}
                                        <button class="layui-btn" lay-submit lay-filter="submit">发布</button>
                                        {else /}
                                        <button class="layui-btn" lay-submit lay-filter="edit">修改</button>
                                        {/if}
                                    </div>
                                </div>
                                <div style="height: 200px;"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </fieldset>
        </div>
    </div>
</div>
<script type="text/javascript" src="/static/layuiadmin/layui/layui.js" ></script>
<!-- 配置文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/static/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/static/js/imgUp.js"></script>
<script type="text/javascript" src="/static/js/imgPlugin.js"></script>
<script>
    //产品图片多图上传
    $("#file").takungaeImgup({
        formData: {
            "name": "img"
        },
        url: "{:url('Product/shopUpload')}",
        success: function(data) {},
        error: function(err) {
            alert(err);
        }
    });

    layui.config({
        version: true,
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'set', 'spec', 'upload', 'colorpicker', 'tableSelect'], function(){
        var $ = layui.jquery,
                form = layui.form,
                upload = layui.upload,
                tableSelect = layui.tableSelect,
                colorpicker = layui.colorpicker;

        //编辑器
        var details = UE.getEditor('details', {
            initialFrameWidth: 800,
            initialFrameHeight: 320
        });

        tableSelect.render({
            elem: '#demo',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索家名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('Product/trader')}",
                method: 'post',
                cols: [[
                    {type: 'radio'},
                    {field: 'name', title: '商家名称', width: 400},
                    {field: 'phone', title: '商家手机', width: 100},
                    {field: 'time', title: '加入时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                $("input[name=genus]").val(field[0].id);
                $("#demo").val(field[0].name);
            }
        });

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            ,color: '#ffffff'
            ,done: function(color){
                $('#test-form-input').val(color);
            }
        });



        //普通图片上传
        upload.render({
            elem: '#demo3'
            ,url: "{:url('Product/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg(res.msg);
                }
                //上传成功
                $("input[name=poster]").val(res.img);
                layer.msg('上传成功');
            }
        });

        if ('{$find.share}'){
            $("#share").show();
        }else{
            $("#share").hide();
        }

        form.on('switch(share)', function(data){
            if (data.elem.checked){
                $("#share").show();
            }else{
                $("#share").hide();
            }
        });

        if ('{$find.is_open}'){
            $(".kalman").removeClass('layui-hide');
        }else{
            $(".kalman").addClass('layui-hide');
        }

        form.on('switch(is_open)', function(data){
            if (data.elem.checked){
                $(".kalman").removeClass('layui-hide');
            }else{
                $(".kalman").addClass('layui-hide');
            }
        });

        if ('{$find.replace_open}'){
            $("#replace").show();
        }else{
            $("#replace").hide();
        }

        form.on('switch(replace_open)', function(data){
            if (data.elem.checked){
                $("#replace").show();
            }else{
                $("#replace").hide();
            }
        });



        $("#kalman").click(function (){
            layer.prompt({
                formType: 0,
                title: '生成数量'
            }, function(value, index, elem){
                if (isNaN(value)){
                    layer.msg('请输入数字');
                    return false;
                }
                layer.close(index);
                var load = layer.load();
                $.post("{:url('Product/kalman')}", {value:value}, function(e){
                    layer.close(load);
                    if (e.info == 1){
                        layer.msg('生成成功');
                        $("textarea[name=kalman]").val('');
                        var html = '';
                        for (var i = 0; i < e.data.length; i++){
                            html += e.data[i] + '\n';
                        }
                        $("textarea[name=kalman]").val(html);
                    }else{
                        layer.msg('生成失败');
                    }
                }, 'json');
            });
        });

        form.verify({
            che0: function(value, item){
                var open = $("input[name=is_open]").prop('checked');
                if (open && !value){
                    return '必填项不能为空';
                }
            },
            che1: function(value, item){ //value：表单的值、item：表单的DOM对象
                var che = $(item).parent().parent().prevAll('input[type=radio]:first:checked').val();
                var open = $("input[name=is_open]").prop('checked');
                if (che === '1' && open){
                    if (!value){
                        return '必填项不能为空';
                    }
                }
            }
        });

        upload.render({
            elem: '#test5'
            ,accept: 'video' //视频
            ,url: "{:url('Product/videoUpload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("input[name=video]").val(res.img);
            }
        });

        //每秒检测最小值和总值
        setInterval(function (){
            var price = [];
            var stock = 0;
            var weight = [];
            $("#tables tbody tr").each(function (){
                var price_zhi = $(this).find('input[name=price]').val();
                if (price_zhi){
                    price.push(price_zhi);
                }
                var stock_zhi = $(this).find('input[name=stock]').val();
                if (stock_zhi){
                    stock += parseInt(stock_zhi);
                }
                var weight_zhi = $(this).find('input[name=weight]').val();
                if (weight_zhi){
                    weight.push(weight_zhi);
                }
            });
            if (price.length > 0){
                var price_min = Math.min.apply(null, price);
                $("#price").val(price_min); //价格
            }
            if (weight.length > 0){
                var weight_min = Math.min.apply(null, weight);
                $("#weight").val(weight_min); //重量
            }
            if (stock > 0){
                $("#stock").val(stock); //库存
            }
        }, 500);

        //自定义验证
        form.verify({
            img: function(value, item){ //value：表单的值、item：表单的DOM对象
                var length = $(".up-section").length;
                if (length == 0){
                    $(item).addClass('layui-form-danger');
                    return '最少上传一张图片';
                }
            }
        });

        var can = "{$can}";
        if (can){
            $("#tables tbody tr").each(function(xu){
                //所有tr的长度
                var trLength = $(this).find('td').length;
                if (trLength > 5){
                    var xunhuan = trLength-5; //当前参数后面有几个参数
                    for (var i=0;i<xunhuan;i++){
                        var huo = xunhuan-i; //循环条件(次数)
                        //获取规格倒数huo两个数加入数组 然后相乘等于rowspan的数
                        var zhi = 1;
                        for (var z=0;z<huo;z++){
                            var chang = $(".gui-content").eq(-(z+1)).find(".gui-flex .spec-relative .zilei").length;
                            zhi = zhi*chang
                        }
                        $(this).find('td').eq(i).attr('rowspan', zhi)
                    }
                }
                //当前规格长度
                var gc = $(".gui-content").length;
                if (gc == trLength - 4){
                    group = [];
                    for (var g=0;g<gc;g++){
                        group.push($(this).find('td').eq(g).html());
                    }
                    group.join(',');
                    $(this).attr('data-group', group);
                }
                if ($(this).prop("className") != 'shezhi'){
                    if (gc != trLength - 4){
                        var duo = trLength-4;
                        for (var d=0;d<duo;d++){
                            group.splice(-(d+1), 1, $(this).find('td').eq(-(d+5)).html());
                        }
                        group.join(',');
                        $(this).attr('data-group', group);
                    }
                }
            });
            $(".kucun").show();
        }

        //规格
        function spec(){
            //声明一个数组用于循环装子类规格
            var canarr = [];
            var zilei = [];
            var img = [];
            $(".gui-content").each(function (index){ //循环规格
                var j = {};
                var name = $(this).find('.layui-this').text();
                zilei[index] = []; //声明是二维数组
                img[index] = []; //声明是二维数组
                $(this).find('.spec-relative').each(function (){ //循环子类
                    zilei[index].push($(this).find('.zilei').text());
                    img[index].push($(this).find('input[name=upload]').val());
                });
                var bei = $(this).find('input[name=zidingyi]').val();
                if (name == '自定义'){
                    j[name] = {name:zilei[index],img:img[index],bei:bei};
                }else{
                    j[name] = {name:zilei[index],img:img[index]};
                }
                if (j[name].name.length > 0){
                    canarr.push(j);
                }
            });
            return canarr;
        }

        //规格值
        function value(){
            var data = [];
            $("#tables tbody tr").each(function (){
                if ($(this).prop("className") != 'shezhi'){
                    var group = $(this).attr('data-group');
                    var price = $(this).find("input[name=price]").val();
                    var stock = $(this).find("input[name=stock]").val();
                    var cost = $(this).find("input[name=cost]").val();
                    var weight = $(this).find("input[name=weight]").val();
                    data.push({group:group,price:price,stock:stock,cost:cost,weight:weight});
                }
            });
            return data;
        }

        //添加
        form.on('submit(submit)', function(data){
            var field = data.field;
            var img = [];
            $.each($('input[name=img]'), function () {
                img.push($(this).val());
            });
            field.img = img;
            field.spec = spec();
            field.value = value();
            field.details = details.getContent();
            $.post("{:url('Product/product_add')}", {field:field}, function(e){
                if (e == 1){
                    layer.msg('添加成功', {icon: 1});
                    setTimeout(function(){
                        parent.layui.index.openTabsPage('/admin/product/product.html', '商品管理');
                    }, 500);
                }else{
                    layer.msg('添加失败', {icon: 2});
                }
            })
        });

        //修改
        form.on('submit(edit)', function(data){
            var field = data.field;
            var img = [];
            $.each($('input[name=img]'), function () {
                img.push($(this).val());
            });
            field.img = img;
            field.spec = spec();
            field.value = value();
            field.details = details.getContent();
            $.post("{:url('Product/product_edit')}", {field:field}, function(e){
                if (e == 1){
                    layer.msg('修改成功', {icon: 1});
                    setTimeout(function(){
                        parent.layui.index.openTabsPage('/admin/product/product.html', '商品管理');
                    }, 500);
                }else{
                    layer.msg('修改失败', {icon: 2});
                }
            })
        });

    });
</script>
</body>
</html>